︠b094f04e-ec07-4f52-b2e1-29031e41ac7ci︠ %html
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
The point of this worksheet is to introduce you to some of the basic commands in Sage. The simplest way I know to learn how to use things like Sage is to copy examples from someone else, and then change them to fit what you need. Throughout this worksheet I have include blank "Sage cells" in which you can type examples and try new things. To evaluate a cell, just click on the cell (its background is then coloured in light blue) and then click the "run"-symbol ("Execute current or selected cells"). Alternatively, you may use Shift+Return. This evaluates the cell.
Important: Even if the output for a Sage cell is already displayed, be sure to evaluate each of the cells as you read along since some of the later cells depend on earlier cells have been executed.
Let's try a simple example. In the empty cell below, type in some complicated expression of numbers (no variables right now) involving addition, subtraction, multiplication, and division. Use $*$ for multiplication and $/$ for division ($//$ for integer divison). When you are done, evaluate it. If you get an error, you must have typed something in incorrectly; just try again.
︡7aef00af-fe79-490f-a261-03c49a16be79︡{"done":true,"html":"This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
\n\nThe point of this worksheet is to introduce you to some of the basic commands in Sage. The simplest way I know to learn how to use things like Sage is to copy examples from someone else, and then change them to fit what you need. Throughout this worksheet I have include blank \"Sage cells\" in which you can type examples and try new things. To evaluate a cell, just click on the cell (its background is then coloured in light blue) and then click the \"run\"-symbol (\"Execute current or selected cells\"). Alternatively, you may use Shift+Return. This evaluates the cell.
\nImportant: Even if the output for a Sage cell is already displayed, be sure to evaluate each of the cells as you read along since some of the later cells depend on earlier cells have been executed.
\nLet's try a simple example. In the empty cell below, type in some complicated expression of numbers (no variables right now) involving addition, subtraction, multiplication, and division. Use $*$ for multiplication and $/$ for division ($//$ for integer divison). When you are done, evaluate it. If you get an error, you must have typed something in incorrectly; just try again.
"} ︠5f70aad4-a179-45d7-a02c-9270e3a80551s︠ 5//2 ︡2d98e2c0-5bb9-4918-ba6c-91518fb97fca︡{"stdout":"2\n"}︡{"done":true}︡ ︠c3eaf019-a8c4-4099-803c-e527495e5cb2i︠ %htmlThe first thing you should always do when beginning a new worksheet is to define the variables you plan to use. Since $x$ and $y$ are usually variables in our work, I'll start by stating they are variables. (Note: Sage always assumes $x$ is variable, but I think this is the only one it assumes anything about.) The "%auto" command makes this cell evaluate every time you load this worksheet, so that you can use $x$ and $y$ as variables without having to evaluate this cell.
︡01791e1c-9867-4bf9-9631-14a326f0521b︡{"html": "The first thing you should always do when beginning a new worksheet is to define the variables you plan to use. Since $x$ and $y$ are usually variables in our work, I'll start by stating they are variables. (Note: Sage always assumes $x$ is variable, but I think this is the only one it assumes anything about.) The \"%auto\" command makes this cell evaluate every time you load this worksheet, so that you can use $x$ and $y$ as variables without having to evaluate this cell.
"}︡ ︠5522f651-4d47-4bd5-81c5-96fcda23b9a5as︠ %auto var('x,y') ︡79376623-e857-4f95-a8b6-97f18778487b︡{"stdout":"(x, y)\n"}︡{"done":true}︡ ︠2b55f685-ff16-49cd-aa85-e96a4318b3adi︠ %htmlMimicking the syntax above, declare another variable in the cell below. Pick your favorite letter, but you should avoid $e$, $i$, and $I$. Why?
︡30e69d29-5641-422d-8eca-38a2b2fb6426︡{"html": "Mimicking the syntax above, declare another variable in the cell below. Pick your favorite letter, but you should avoid $e$, $i$, and $I$. Why?
"}︡ ︠89c6d151-bab8-4743-8248-49558d42f2a4i︠ %htmlYou can insert a new cell above or below any existing cell or block of text. To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a purple or blue (depending on your browser) horizontal line pop up. Click on this horizontal line to produce a new Sage cell. Try that just below (between this block of text and the next Sage cell) right now and then type in some other complicated expression of numbers and then click "evaluate."
You can insert a new cell above or below any existing cell or block of text. To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a purple or blue (depending on your browser) horizontal line pop up. Click on this horizontal line to produce a new Sage cell. Try that just below (between this block of text and the next Sage cell) right now and then type in some other complicated expression of numbers and then click \"evaluate.\"
\nYou can also turn a cell into a text cell. For this put
%md
at the top of a cell to use markdown formatting (which fully supports LaTeX formulae enclosed in $\$$ for inline math or $\$\$$ for equations). See http://daringfireball.net/projects/markdown/ about markdown. When you press shift-enter, the output is displayed. Double click to edit.
You can also use %html in a similar way to the above, which also supports LaTeX.
Add a text cell at the very top of this worksheet and type in your names. Do that right now and then continue reading where you left off.
Let's try to solve an equation. Put your cursor in the Sage cell below and click evaluate.
︡fed47b4c-445b-4e05-a00d-f51a358c1fa5︡{"html": "You can also turn a cell into a text cell. For this put
\n%md
\nat the top of a cell to use markdown formatting (which fully supports LaTeX formulae enclosed in $\\$$ for inline math or $\\$\\$$ for equations). See http://daringfireball.net/projects/markdown/ about markdown. When you press shift-enter, the output is displayed. Double click to edit.
\nYou can also use %html in a similar way to the above, which also supports LaTeX.
\nAdd a text cell at the very top of this worksheet and type in your names. Do that right now and then continue reading where you left off.
\nLet's try to solve an equation. Put your cursor in the Sage cell below and click evaluate.
"}︡ ︠4a05200f-b7b6-4fa0-a45b-3670bcbd6b83︠ solve(x^2-4*x+3==0,x) ︡00877036-f667-418f-83d0-2b9872b937a8︡︡ ︠8390f317-3067-4889-bf89-e54f128348eei︠ %htmlNotice the use of "==" in this context. You'll always need to use this syntax when defining an equation. A single "=" is used for assignments (like variable assignments). Also, notice that I used a * to denote multiplication. If you write "4x" without the *, you will get an error message. To see what I mean, click evaluate on the cell below.
︡461b24ff-22e6-4e46-8b75-9242920dd2c5︡{"html": "Notice the use of \"==\" in this context. You'll always need to use this syntax when defining an equation. A single \"=\" is used for assignments (like variable assignments). Also, notice that I used a * to denote multiplication. If you write \"4x\" without the *, you will get an error message. To see what I mean, click evaluate on the cell below.
"}︡ ︠307254e4-30bd-4a3b-8e17-741877521fb8︠ solve(x^2-4x+3==0,x) ︡d3cf5a07-901c-4a86-9f80-4e6f42347b10︡︡ ︠685c0435-95eb-4fe2-9026-393beae9a321i︠ %htmlNow, I want you to try one. Mimic the example above that works, being sure to use * for multiplication and two equal signs. Also, try using the variable you defined earlier instead of $x$.
︡ccc06591-cd75-4c8e-aa20-66bb6eec02a6︡{"html": "Now, I want you to try one. Mimic the example above that works, being sure to use * for multiplication and two equal signs. Also, try using the variable you defined earlier instead of $x$.
"}︡ ︠8f4df9bf-360c-424e-bdb4-d141dee3611ei︠ %htmlYou can quickly factor polynomials in Sage using the "factor" command. Evaluate each of the next two cells.
︡ca8ee8e7-9e38-4193-ac49-8bac1976a791︡{"html": "You can quickly factor polynomials in Sage using the \"factor\" command. Evaluate each of the next two cells.
"}︡ ︠ef1f0134-11c8-4e6b-80f3-da090637f246︠ factor(x^2+3*x+2) ︡48b92cc6-1a34-4b91-8464-958f43d36493︡︡ ︠da7fe747-8fe9-4ad9-ac1e-ede2c441fe37︠ factor(x^2+3x+2) #without the * between 3 and x, there is an error. ︡6393e2ee-4c8f-4161-95ba-4e7238dd1387︡︡ ︠5e31405d-f276-4fba-9c55-25e97ff390d5i︠ %htmlNote: You can use # to make comments inside a Sage cell. Sage will ignore anything to the right of #. For example, Sage ignored the comment "#without the * between 3 and x, there is an error." in the previous cell.
You can also use the "factor" command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms. Click evaluate on the cell below.
︡b9bdcfe2-03bd-4d37-ba2c-c183b5b48033︡{"html": "Note: You can use # to make comments inside a Sage cell. Sage will ignore anything to the right of #. For example, Sage ignored the comment \"#without the * between 3 and x, there is an error.\" in the previous cell.
\nYou can also use the \"factor\" command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms. Click evaluate on the cell below.
"}︡ ︠9cf69a5f-ad80-4cdc-9148-9e489d1e18e4︠ factor((x^2+4*x+3)/(x^2-9)) ︡43efc243-2c66-4b1c-9435-174812344f10︡︡ ︠c2b82c17-a66e-4b2f-8e89-de9856fdffeei︠ %htmlIn the cell below, we illustrate the use of the "expand" command, which is essentially the opposite of the "factor" command.
︡f9b09296-3e3d-42f4-a37c-976a5d96e385︡{"html": "In the cell below, we illustrate the use of the \"expand\" command, which is essentially the opposite of the \"factor\" command.
"}︡ ︠8d74db9d-5547-4878-bc23-f75cdefc9303︠ expand((x+3)^7) ︡0afb84e3-c285-444b-b8f1-ac649efb0ec8︡︡ ︠2b85abe9-8fb2-49fc-8535-ba05c7831d04i︠ %htmlOK, your turn. Try a few examples on your own that incorporate the "factor" and "expand" commands.
︡a7953c34-ba66-4f18-9f54-066dee3adf50︡{"html": "OK, your turn. Try a few examples on your own that incorporate the \"factor\" and \"expand\" commands.
"}︡ ︠0eb940d8-2cf8-4dbc-ba6f-ab3123716835i︠ %htmlIf you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable. In the cells below, I store a rational expression as $f$, simplify it, and then print the original expression. Notice my use of the command "f.full_simplify()." In Sage, you can often write "f.some_command()" to perform operations on $f$. This syntax is very useful and we'll discuss this notation in more detail later.
︡8411cf05-2fe4-4e71-955b-fdd810442059︡{"html": "If you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable. In the cells below, I store a rational expression as $f$, simplify it, and then print the original expression. Notice my use of the command \"f.full_simplify().\" In Sage, you can often write \"f.some_command()\" to perform operations on $f$. This syntax is very useful and we'll discuss this notation in more detail later.
"}︡ ︠dd74dfe9-f69a-4f99-ad87-c07639720141︠ f=(x^2+4*x+3)/(x^2-9) f.full_simplify() ︡5de25d09-5bad-4c0b-a46b-ef980b8f1034︡︡ ︠1ed792dc-612f-456b-b18e-900195cef7d5︠ f ︡6896dc3c-aac3-4069-989b-0c3ec61b471f︡︡ ︠58cab1f3-dede-4279-9402-0ddd24826540i︠ %htmlPerhaps you'd like to save the simplified version of $f$ for later use. You can do this by assigning it a name, like $g$. See if you can figure out how to do this in the cell below.
︡61eb1aed-0919-4267-9e27-250a310ae23e︡{"html": "Perhaps you'd like to save the simplified version of $f$ for later use. You can do this by assigning it a name, like $g$. See if you can figure out how to do this in the cell below.
"}︡ ︠1a97b969-8180-4a75-b290-c7a624d9bb85i︠ %htmlLet's try some plotting. We can plot a function determined by an expression by using the "plot" command. The "plot" command has many options, but at the very least, we should specify the independent variable and a range for this variable. Evaluate the cell below.
︡bc5044f4-3d5f-4db0-836d-bd3c365a84be︡{"html": "Let's try some plotting. We can plot a function determined by an expression by using the \"plot\" command. The \"plot\" command has many options, but at the very least, we should specify the independent variable and a range for this variable. Evaluate the cell below.
"}︡ ︠368769c7-fea1-4fdc-931f-f8c8b05ae58d︠ plot(f,-2,4) ︡bc2e28cf-f2d0-41a3-bcb9-b7c06e0a7694︡︡ ︠0112fb59-e269-4f6a-a1f9-50dc18e70053i︠ %htmlPerhaps you don't like the choice that Sage made for the range on the $y$-axis. Using the "ymin" and "ymax" options of the "plot" command, you can choose a top and bottom range for your plot. Both commands below produce the same output.
︡0158fc4e-4f94-4c7b-9564-b91514db69e7︡{"html": "Perhaps you don't like the choice that Sage made for the range on the $y$-axis. Using the \"ymin\" and \"ymax\" options of the \"plot\" command, you can choose a top and bottom range for your plot. Both commands below produce the same output.
"}︡ ︠ca108af0-615b-4a3e-94ae-83c979741da2︠ plot(f,(x,-2,4),ymin=-10,ymax=10) ︡9fc3adf8-6ce0-4cdf-9f53-c3684cac3dd6︡︡ ︠5a9473d3-942a-47cc-bb43-0519b9c41ca1︠ f.plot((x,-2,4),ymin=-10,ymax=10) ︡2f185b35-a4ea-49bd-8e85-fb4351523dae︡︡ ︠9a6407a6-04ff-4e8a-b59d-4921df16665fi︠ %htmlLet's look at some more plotting examples.
︡7a7eb44d-ff9f-4258-8f57-341307e87f87︡{"html": "Let's look at some more plotting examples.
"}︡ ︠d376a77b-278b-4cb4-b9ca-9166f437a853︠ #this is the basic plotting command plot(x^2, (x, -2, 3)) ︡77129e52-4b3e-48e8-96b7-aa8ddfadb723︡︡ ︠eeb314fc-0ba8-48fe-bb6f-ff6b3acf1370︠ #here's an example, where we use a different variable and give the expression a name first var('t') p=t^2-3*t plot(p,(t,-1,1)) ︡aad7149a-f62c-4878-9f31-2d43b77ad5aa︡︡ ︠920dd58d-3460-4a4b-8cbd-6f0954beeee7︠ #You can include multiple functions in the plot command plot((x^2,sqrt(x),sin(x)), (x, 0, pi)) ︡e2e1b1ea-7b09-4378-a76e-9dc9b31ff74c︡︡ ︠d316f5ac-4fbc-4049-8e4f-0f416f4d11d7︠ #You can also combine plots by adding them. This allows us to make each plot a different color. plot(x^2, (x, 0, pi),color='red')+plot(sqrt(x), (x, 0, pi),color='green')+plot(sin(x), (x, 0, pi)) ︡69172b8d-44b1-4233-a1e9-5815385638a8︡︡ ︠77bfb0b3-c32d-433a-9bdf-516f85a9dd42i︠ %htmlTry plotting your own function below.
︡a79e33a9-2045-49f9-a431-dfcd5ef2bbfa︡{"html": "Try plotting your own function below.
"}︡ ︠55c298ba-1163-4efe-a08c-ad259f1bc8f9i︠ %htmlOnce you have defined a variable, if you want to remove it from Sage's memory use the "del()" function. The following command removes $f$ from memory, so typing "f" from now on should give an error.
︡ed253c18-e121-4e9b-8bed-8ea70df84280︡{"html": "Once you have defined a variable, if you want to remove it from Sage's memory use the \"del()\" function. The following command removes $f$ from memory, so typing \"f\" from now on should give an error.
"}︡ ︠7542fb93-b949-4ec3-9f79-5892da20c393︠ del(f) ︡73be1303-dcbd-4043-a052-eecab0f9e23d︡︡ ︠28439ef5-7ec9-4f2c-83e2-8936f124f8f5︠ f ︡c2ecea7b-63d2-4fea-b86e-4cb7b5342067︡︡ ︠775d5488-e7ac-4c18-be35-27872db62091i︠ %htmlTo use function notation in Sage (so that you can write something like "f(2)") just begin with "f(x)=" before writing the expression that defines the function. Then you can use "f(2)", "f(a)", "f(x+h)" or any kind of function notation that you are familar with from algebra. Evaluate each of the cells below.
︡5abee6a6-4e4a-42ed-ac72-5f40059c10ae︡{"html": "To use function notation in Sage (so that you can write something like \"f(2)\") just begin with \"f(x)=\" before writing the expression that defines the function. Then you can use \"f(2)\", \"f(a)\", \"f(x+h)\" or any kind of function notation that you are familar with from algebra. Evaluate each of the cells below.
"}︡ ︠2c6f9ad5-0fa8-4852-8562-af333614a02c︠ f(x)=5*x^2+4 ︡462eb6ef-30c8-4afd-8a5a-716907cbd08e︡︡ ︠fd406e03-39d2-47f6-8815-0459065e1790︠ f(x) ︡029e2eb7-57a8-400d-b31b-c811facae034︡︡ ︠ea63d297-97e7-4da8-9b95-009a49b03129︠ f ︡61cb7244-93eb-46a9-8fde-653bfa8078a5︡︡ ︠467fa8fb-f896-44cb-9d75-ba637ab2cb08︠ f(4) ︡f7684408-c371-4ddc-9007-76a452d84563︡︡ ︠c90303df-0f69-49f2-b0e7-349ba59fb92f︠ a=4 ︡b4468dbf-c218-4267-9f78-5189d2527292︡︡ ︠2539888e-e5a0-4e45-b750-b53f70968b71︠ f(a) ︡047bad76-709c-40fb-9e19-7a129845b242︡︡ ︠0c61f951-626a-4527-850b-ffa93a276f12︠ g(x)=x^2-3*x+2 f(g(x)) ︡35bc7e72-c12b-4aa1-b3cc-9a55c3ff9886︡︡ ︠07bd9a4e-8c70-4cd6-b585-c0fece9a9b6ci︠ %htmlIn the cells below, we illustrate another use of the "expand" command.
︡3de597c7-0bfc-43f5-87f6-28fda1659ee2︡{"html": "In the cells below, we illustrate another use of the \"expand\" command.
"}︡ ︠32014e80-5e96-4351-a842-fc8587b075b2︠ expand(g(f(x))) ︡336d874d-5021-430d-9f6a-0f808dfeac7b︡︡ ︠c4856362-1427-4bef-b1e0-5110bca0e867︠ g(f(x)).expand() #this is just a different way of writing the command above. ︡8418be84-1bc1-4eac-9b7b-5c811cd478a5︡︡ ︠43f5c5da-6176-46a1-9dd9-68cc653fb4cai︠ %htmlDo some experimenting with your own examples in the cells below. Don't worry about doing it incorrectly. You'll learn a lot in the process of "screwing up."
︡63648431-c68c-4a2a-9e17-576759301c2d︡{"html": "Do some experimenting with your own examples in the cells below. Don't worry about doing it incorrectly. You'll learn a lot in the process of \"screwing up.\"
"}︡ ︠1936ddfa-79f6-49c5-b517-f77672d5a0b1i︠ %htmlSage will try to return symbolic answers when it can. You can use the "show()" command to get a pretty output for individual cells. To see what I mean, click "evaluate" on the next three cells.
︡d1abd0e2-3527-49ba-a301-571157d636f1︡{"html": "Sage will try to return symbolic answers when it can. You can use the \"show()\" command to get a pretty output for individual cells. To see what I mean, click \"evaluate\" on the next three cells.
"}︡ ︠50fafc26-519f-4b03-aa0c-cb3b11e74724︠ sqrt(2) ︡58e727b4-3d6f-4260-bc99-c5656f007471︡︡ ︠34bf0b4a-cb65-40b1-a08d-312ea6324164︠ show(sqrt(2)) ︡f20c35b3-bbca-4657-bfb3-fa2eb55f8dd8︡︡ ︠984b14eb-7f18-497f-a161-7e87ed945e8a︠ sqrt(2).show() #this is just another way of writing the command ︡38c1b591-4f7c-45c0-88e7-1a8011100d48︡︡ ︠2f47f8fb-c52e-4401-aa47-f6543398944bi︠ %htmlWe can also use the "n()" to get a numeric value. Below I show you mutliple ways to use the "n()" function.
︡fd9c9803-66e9-4ef1-9389-8edbea8e8036︡{"html": "We can also use the \"n()\" to get a numeric value. Below I show you mutliple ways to use the \"n()\" function.
"}︡ ︠4fc62edb-3e9c-4716-b445-b726bea99401︠ sqrt(2).n() ︡272b5518-5e03-4733-a593-8991760cd907︡︡ ︠22a0831d-d6c1-4aa3-a39c-e464e4266129︠ n(sqrt(2)) #this cell and the previous are two different ways of writing the same command ︡6b45eb4b-d98b-4826-8c1c-c29048b681bb︡︡ ︠2808f624-4ff2-4f3f-bcfa-99cee5fe83e8i︠ %htmlWhat if I want more digits of $\sqrt{2}$?
︡e00b6222-b8ad-49c6-9d3e-a59018318a53︡{"html": "What if I want more digits of $\\sqrt{2}$?
"}︡ ︠25519989-55c5-4a92-a819-5af83e583a9d︠ sqrt(2).n(digits=50) ︡b9df72ff-20ce-4287-a349-52fbc07bea8e︡︡ ︠8bf4366e-5fc5-490e-8717-350b2f93ec1c︠ n(sqrt(2),digits=500) #Just for fun let's look at 500 decimals. ︡e7715dc4-f1cc-471f-9d9d-f1d8e81a2576︡︡ ︠a2f1be97-fde2-495b-8d1d-153a0005bbc5i︠ %htmlTry similar things with the number $e$.
︡0ebf51d6-76b5-4442-aef1-146df85b41cf︡{"html": "Try similar things with the number $e$.
"}︡ ︠3d302aa0-f276-496c-b3cf-042b7df7ccffi︠ %htmlNow let's look at some trigonometry. Sage will give you an exact value, and simplify it when possible. Since the exact value of sin(2) is unknown, it just returns sin(2) unless you ask for a number.
︡a61b6629-b6f6-4991-bf43-12f8f79f256b︡{"html": "Now let's look at some trigonometry. Sage will give you an exact value, and simplify it when possible. Since the exact value of sin(2) is unknown, it just returns sin(2) unless you ask for a number.
"}︡ ︠e09a5d09-17d6-4300-9535-eb40a726d1f5︠ cos(pi/2) ︡7caf4112-a0da-497e-9ec0-852af13ecb06︡︡ ︠1e0e1285-36b5-40f7-ac7b-1af33549eb54︠ cos(pi/6) ︡48a6a954-9db6-4359-ae47-e35dc566875f︡︡ ︠af592e57-29a7-47f6-a2a8-3d6d96d23560︠ sin(2) ︡a89eed71-c8aa-4c9d-be9f-0c505e4b4458︡︡ ︠8fc41025-5cea-4abe-86d7-092f8046228b︠ sin(2).n() ︡fafb6cfe-1785-4b44-b828-bd051220f6d7︡︡ ︠2a5be394-25c1-44e5-bddc-73cee3b1aa94︠ cos(pi/12) ︡91a43e87-8f62-41cf-8508-7c6c64d769c2︡︡ ︠36161620-3a07-41fa-8404-96b5e60b2de8i︠ %htmlYou can use Sage to help you remember some of those complicated trig identities.
︡2fc32453-0edc-428e-b1a7-d5311b68e106︡{"html": "You can use Sage to help you remember some of those complicated trig identities.
"}︡ ︠b30410b9-bc73-4bcf-9633-e3cf3b4396f6︠ g=cos(x+y) ︡5cbff400-4265-48cd-82ff-0d923702e13e︡︡ ︠38030252-4a7d-4d4e-af34-50bddfb8ffe2︠ g.expand_trig() #You can use Sage to help you remember your trig identities. ︡f7e650b9-7df4-4b91-b381-01bb792b8dc1︡︡ ︠568ab5a7-9568-4589-91b0-e65c9e260bf8i︠ %htmlTry some of your own examples.
︡8ca22d2d-dbc4-45df-8280-34476b361efc︡{"html": "Try some of your own examples.
"}︡ ︠efca2b59-cb2c-45ab-9797-5e778365d504i︠ %htmlThis final example shows you a feature of Sage called and "interact", which allows you to create a slider to change variables. Drag the sliders around and experiment.
︡3562c15a-8093-4a4d-aced-087185ca38f2︡{"html": "This final example shows you a feature of Sage called and \"interact\", which allows you to create a slider to change variables. Drag the sliders around and experiment.
"}︡ ︠3a3ae803-9860-40ae-9ae1-b3f6f71723b7︠ @interact def _(a=(-2,2),b=(1/2,3,1/4),c=(-pi,pi,pi/6),d=(-2,2)): show(plot(a*sin(b*x+c)+d,(x,0,6),ymin=-5,ymax=5)+plot(sin(x),(x,0,6),ymin=-5,ymax=5,color='red')) ︡01f17300-2c86-4ae3-bfc1-3ac987cda1b3︡︡ ︠1b284cdb-88cc-4e99-b8ac-5337553b5479i︠ %htmlThankfully, you don't usually need to understand the code that constructs an interact. You just have to be able to fiddle with the sliders and such.
There are several methods for getting help while using a Sage Worksheet. The most obvious one is to click on the Help link at the top of the page. This takes you to the Sage documentation. Almost everything you ever need to know can be found there in one way or another. I say almost because Sage is an open-source project maintained by a community of mathematicians across the world. Some people are better than others at writing documentation. Also, it is a bit intimidating that "Almost everything you ever need to know" is there! Sage is a large project with a lot of power and functionality. It can be difficult to find the thing you need unless you know how to look for it.
Thankfully, the Sage Developers have incoporated other ways to get help. The first option is called Tab Completion. You can type part of a command and then hit the [Tab] key to get a menu of possible commands that start that way. Let's try it. In the next cell type
A = ma
then hit the [Tab] key.
︡ab2c8e82-5157-4a91-b03c-a90fa5271ec0︡{"html": "Thankfully, you don't usually need to understand the code that constructs an interact. You just have to be able to fiddle with the sliders and such.
\n\n
There are several methods for getting help while using a Sage Worksheet. The most obvious one is to click on the Help link at the top of the page. This takes you to the Sage documentation. Almost everything you ever need to know can be found there in one way or another. I say almost because Sage is an open-source project maintained by a community of mathematicians across the world. Some people are better than others at writing documentation. Also, it is a bit intimidating that \"Almost everything you ever need to know\" is there! Sage is a large project with a lot of power and functionality. It can be difficult to find the thing you need unless you know how to look for it.
Thankfully, the Sage Developers have incoporated other ways to get help. The first option is called Tab Completion. You can type part of a command and then hit the [Tab] key to get a menu of possible commands that start that way. Let's try it. In the next cell type
A = ma
\nthen hit the [Tab] key.
"}︡ ︠a9a45c6c-2d2a-4f4a-a3d9-6788ea831eabi︠ %htmlNotice that there are quite a few commands that start with "ma". We want the one for a matrix. Can you find it?
Define a matrix called $A$ by typing the following in the next cell, and then evaluating it.
A = matrix(ZZ, [[1,0],[1,1]])
︡0e3fbdf9-8716-42da-a7f0-b988c6159a8c︡{"html": "Notice that there are quite a few commands that start with \"ma\". We want the one for a matrix. Can you find it?
\nDefine a matrix called $A$ by typing the following in the next cell, and then evaluating it.
\nA = matrix(ZZ, [[1,0],[1,1]])
"}︡ ︠b32cb2cb-80aa-40f3-87a7-3bb5c7fdadfei︠ %htmlNote that nothing seems to happen. Sage made the matrix and put it in memory, but didn't print it out. To see it, simply type $A$ in a cell and evaluate.
︡eed1eb32-e5cd-4020-b4b2-bc12da5de6ab︡{"html": "Note that nothing seems to happen. Sage made the matrix and put it in memory, but didn't print it out. To see it, simply type $A$ in a cell and evaluate.
"}︡ ︠3c017a4f-21fc-4ed9-8bdc-2ea9c7402273i︠ %htmlThere is a fundamental number attached to each matrix called its "rank". Sage has a way to compute the rank built in. Like most built-in methods, you can access this function in two ways. First type
rank(A)
and evaluate. Then make a new cell and type
A.rank()
and evaluate. Both times you should get the number $2$.
︡ec19156c-1daf-43be-af95-eb1a8f071606︡{"html": "There is a fundamental number attached to each matrix called its \"rank\". Sage has a way to compute the rank built in. Like most built-in methods, you can access this function in two ways. First type
\nrank(A)
\nand evaluate. Then make a new cell and type
\nA.rank()
\nand evaluate. Both times you should get the number $2$.
"}︡ ︠60053add-1e14-473a-aa01-6de448659742i︠ %htmlNow, what else can you do with a matrix? Sage thinks like a mathematician! In particular, it attaches "methods" to "objects" depending on their "type."
To see all of the methods attached to an object, you can use the "dot- [Tab]" approach. Type "A." (notice the period!) then press [Tab] to get a list of all of the methods attached to the matrix A. Sage knows a lot of linear algebra, so the menu will be very big.
Now, what else can you do with a matrix? Sage thinks like a mathematician! In particular, it attaches \"methods\" to \"objects\" depending on their \"type.\"
\nTo see all of the methods attached to an object, you can use the \"dot- [Tab]\" approach. Type \"A.\" (notice the period!) then press [Tab] to get a list of all of the methods attached to the matrix A. Sage knows a lot of linear algebra, so the menu will be very big.
\nThe other ways to get help involve using the question mark key. First, a command name followed by a question mark will evaluate to the documentation for that command.
I left the matrix command a little bit mysterious above. To see why I had you type it like that, evaluate this in the next cell:
matrix?
The other ways to get help involve using the question mark key. First, a command name followed by a question mark will evaluate to the documentation for that command.
\nI left the matrix command a little bit mysterious above. To see why I had you type it like that, evaluate this in the next cell:
\nmatrix?
\nTo get more information, you can use the double question mark. When you evaluate the command matrix?? you will get the Sage source code for how the matrix command works! I don't use this very often, but if you think you find a bug in Sage, this is the place to start looking. Try it now to see what it looks like. Evaluate
matrix??
To get more information, you can use the double question mark. When you evaluate the command matrix?? you will get the Sage source code for how the matrix command works! I don't use this very often, but if you think you find a bug in Sage, this is the place to start looking. Try it now to see what it looks like. Evaluate
\nmatrix??
\nSage is built on Python, a wonderful modern programming language. You can use any Python code you want to do your mathematics. For example, I used some Python list comprehensions (which look a lot like set-builder notation) to make the following list.
︡fa609c55-a185-4f3f-9eda-86c6c795b3fa︡{"html": "Sage is built on Python, a wonderful modern programming language. You can use any Python code you want to do your mathematics. For example, I used some Python list comprehensions (which look a lot like set-builder notation) to make the following list.
"}︡ ︠92b708b1-0f4a-4272-8d05-0734c3973315︠ [3*n for n in range(17)] ︡9124a04b-6850-4526-a867-9293a80b84d2︡︡ ︠befa2a2d-68c0-4792-b133-7e679282f6ddi︠ %htmlHere is another small bit of Python programming. This time it defines a simple function, and then calls that function to do a computation.
︡61fc085e-eb38-4622-bf2e-0c89442449d9︡{"html": "Here is another small bit of Python programming. This time it defines a simple function, and then calls that function to do a computation.
"}︡ ︠28e85e0d-2b66-4fef-8d5a-10d686854ee5︠ def square_and_add(x,y): z = x^2 + y^2 return z square_and_add(3,4) ︡af971575-d17a-48ec-bd3f-cc3255cf95ac︡︡ ︠107d4126-3c23-456c-9d61-995ce9639fefi︠ %htmlJust to wet your appetite, here is one last example to illustrate Sage's 3D graphing capabilities. Try clicking on the image to rotate it around.
︡517dccfb-31cb-4992-a30b-eda9fa54f6de︡{"html": "Just to wet your appetite, here is one last example to illustrate Sage's 3D graphing capabilities. Try clicking on the image to rotate it around.
"}︡ ︠46e485b9-821a-4e11-b143-732faaf34f81s︠ var('x,y,z'); T = golden_ratio p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x)); r = 4.78 implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=50) ︡2e6bb0a3-deb8-4f3d-8f37-4604506754c1︡ ︠20151332-5db1-406c-8c5f-8cc661fd3cd5i︠ %htmlAll that you have left to do is to show your worksheet to me!
︡7a3165fe-e45e-44b1-a4a3-cf0a00738171︡{"html": "All that you have left to do is to show your worksheet to me!
"}︡